package com.qbccn.bbs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qbccn.bbs.domain.BbsUser;
import com.qbccn.bbs.domain.BbsUserLabel;
import com.qbccn.bbs.domain.BbsUserRole;
import com.qbccn.bbs.domain.BbsUserSign;
import com.qbccn.bbs.domain.BbsUserSignSub;
import com.qbccn.bbs.domain.LogFlyKiss;
import com.qbccn.bbs.dto.ResponseResult;
import com.qbccn.bbs.mapper.BbsUserCountMapper;
import com.qbccn.bbs.mapper.BbsUserLabelMapper;
import com.qbccn.bbs.mapper.BbsUserMapper;
import com.qbccn.bbs.mapper.BbsUserRoleMapper;
import com.qbccn.bbs.mapper.BbsUserSignMapper;
import com.qbccn.bbs.mapper.BbsUserSignSubMapper;
import com.qbccn.bbs.mapper.LogFlyKissMapper;
import com.qbccn.bbs.service.UserService;
import com.qbccn.bbs.utils.CommonUtils;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author linke
 * @date 2020/5/30 19:44
 * @description
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private HttpServletRequest request;
  @Autowired
  private BbsUserMapper bbsUserMapper;
  @Autowired
  private BbsUserRoleMapper bbsUserRoleMapper;
  @Autowired
  private BbsUserCountMapper bbsUserCountMapper;
  @Autowired
  private BbsUserSignMapper bbsUserSignMapper;
  @Autowired
  private BbsUserLabelMapper bbsUserLabelMapper;
  @Autowired
  private BbsUserSignSubMapper bbsUserSignSubMapper;
  @Autowired
  private LogFlyKissMapper logFlyKissMapper;

  @Override
  public BbsUser getUser(String username) {
    return bbsUserMapper.getUser(username);
  }

  @Override
  public BbsUser getUserById(Integer id) {
    return bbsUserMapper.selectByPrimaryKey(id);
  }

  @Override
  public List<BbsUserRole> getRoles(Integer userId) {
    return bbsUserRoleMapper.getRoles(userId);
  }

  @Override
  public Integer getLXSignDayNumber() {
    String username = request.getSession().getAttribute("username").toString();
    BbsUserSignSub sub = bbsUserSignSubMapper.getUserSignSub(username);
    if (sub == null) {
      return 0;
    }
    return sub.getSuccessionDay();
  }

  @Override
  public ResponseResult sign(String username) {
    if (bbsUserSignMapper.hasSign(username)) {
      return new ResponseResult(-1, "您今天已经签到过了！");
    }
    int lx = 0;
    //连续签到数
    BbsUserSignSub bbsUserSignSub = bbsUserSignSubMapper.getUserSignSub(username);
    lx = bbsUserSignSub.getSuccessionDay();
    int num = 5;
    if (lx < 5) {
    } else if (lx >= 5 && lx < 15) {
      num = 10;
    } else if (lx >= 15 && lx < 30) {
      num = 15;
    } else if (lx >= 30) {
      num = 20;
    }
    BbsUserSign sign = new BbsUserSign();
    sign.setUsername(username);
    sign.setCreateTime(new Date());
    sign.setFlyKiss(num);
    sign.setDescription(CommonUtils.getIpAddress(request));
    bbsUserSignSubMapper.updateSignDay(username, bbsUserSignSub.getSuccessionDay() + 1);//更新连续签到
    bbsUserSignMapper.insertSelective(sign);//插入签到
    bbsUserCountMapper.sign(username, num);//kiss增加
    JSONObject object = new JSONObject();
    object.put("get", num);
    object.put("lx", lx);
    //日志记录
    LogFlyKiss kiss = new LogFlyKiss();
    kiss.setGetUsername(username);
    kiss.setStatus(1);
    kiss.setActionType(1);
    kiss.setNumber(num);
    kiss.setCreateTime(new Date());
    logFlyKissMapper.insertSelective(kiss);//kiss日志
    return new ResponseResult(0, "签到成功！", object);
  }

  @Override
  public boolean signStatus(String username) {
    boolean signStatus = bbsUserSignMapper.hasSign(username);
    //如果未签到，判断是否保持连续，如果不连续，置 bbs_user_sign_sub 表连续签到为0
    if (signStatus == false) {
      bbsUserSignSubMapper.updateSuccessionDayToZero(username);
    }
    return signStatus;
  }

  @Override
  public BbsUserLabel getUserLabel(String username) {
    return bbsUserLabelMapper.get(username);
  }

  @Override
  public Integer getFlyKissNumber(String username) {
    return bbsUserCountMapper.getFlyKissNumber(username);
  }

  @Override
  @Transactional
  public ResponseResult updateBaseInfo(BbsUser user) {
    BbsUser u = (BbsUser) request.getSession().getAttribute("user");
    user.setId(u.getId());
    user.setUpdateTime(new Date());
    bbsUserMapper.updateByPrimaryKeySelective(user);
    BbsUser bbsUser = bbsUserMapper.getUser(u.getUsername());
    bbsUser.setPassword(null);
    request.getSession().setAttribute("user", bbsUser);
    return new ResponseResult(0, "更新成功！");
  }

  @Override
  @Transactional
  public ResponseResult updatePassword(String newPassword) {
    String username = request.getSession().getAttribute("username").toString();
    bbsUserMapper.updatePassword(username, new BCryptPasswordEncoder().encode(newPassword));
    return new ResponseResult(0, "更新密码成功！");
  }

  @Override
  public String getPassword() {
    String username = request.getSession().getAttribute("username").toString();
    return bbsUserMapper.getPassword(username);
  }
}
